import { tbSysAttachmentDownload } from '@/api/http'
import { sysConcernedLogAdd } from '@/api/home/sysConcernedLog'
export default {
  methods: {
    downloadCount(resId) {
      let data = {
        type: 3,
        resId,
        opt: 'download',
      }
      sysConcernedLogAdd(data).then((res) => {})
    },
    //多个下载
    download(data) {
      let { level } = this.$store.getters
      if (level != 1) {
        this.warnMsg('抱歉，您没有下载权限')
        return
      }
      //判断是否登录
      if (!this.$store.getters.userId) {
        this.loginTip()
        return
      }
      if (Array.isArray(data)) {
        if (data.length) {
          data.forEach((v) => {
            let fileId = v.fileId
            let fileName = v.sysAttachment.oldName
            this.downloadCall(fileId, fileName)
          })
        } else {
          this.$message({
            message: '请选择文件',
            type: 'warning',
          })
        }
      }
    },
    //单个下载
    downloadCall(fileId, fileName) {
      // let { level } = this.$store.getters
      // if (level != 1) {
      //   this.warnMsg('抱歉，您没有下载权限')
      //   return
      // }
      //判断是否登录
      if (!this.$store.getters.userId) {
        this.loginTip()
        return
      }
      tbSysAttachmentDownload(fileId).then((res) => {
        if (!res.data) return
        this.saveAs(fileName, res.data)
        this.downloadCount(fileId)
      })
    },
    saveAs(name, data) {
      var urlObject = window.URL
      var export_blob = new Blob([data])
      var save_link = document.createElement('a')
      save_link.href = urlObject.createObjectURL(export_blob)
      save_link.download = name
      save_link.click()
    },
  },
}
